﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TurboTemplate
{
    public partial class ListEditorDialog : Form
    {
        private ListWithName m_List;

        public ListEditorDialog(ListWithName list)
            : this()
        {
            m_List = list;

            // Translate into text!

            foreach (var line in m_List.ChildLists)
            {
                txtTransform.Text += line + Environment.NewLine;
                ListToText(line, 1);
            }
            txtTransform.Text = txtTransform.Text.TrimEnd();
        }

        private void ListToText(ListWithName parent, int level)
        {
            foreach (var line in parent.ChildLists)
            {
                txtTransform.Text += new String('\t', level) + line + Environment.NewLine;
                ListToText(line, level + 1);
            }
        }

        public ListEditorDialog()
        {
            InitializeComponent();
        }

        private void ListEditorDialog_Load(object sender, EventArgs e)
        {
            txtTransform.Focus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var doc = txtTransform.Document;
            var parent = m_List;
            var lastList = m_List;
            int lastTabLevel = -1;

            m_List.ChildLists.Clear();
            if (doc.TextLength > 0)
            {

                for (int i = 0; i < doc.TotalNumberOfLines; i++)
                {
                    var segment = doc.GetLineSegment(i);
                    int tabs = CountTabsForSegment(segment);
                    string text = doc.GetText(segment).TrimStart();

                    if (!string.IsNullOrWhiteSpace(text))
                    {
                        if (tabs > lastTabLevel) // Add a child list
                        {
                            // Only create a child level if it is nested. If not, then make a sibling. Work out the tabs...
                            int siblingTabs = CountTabsForSegment(doc.GetLineSegment(i + 1));
                            if (siblingTabs > tabs)
                            {
                                ListWithName childList = new ListWithName(text);
                                childList.Parent = parent;
                                parent.ChildLists.Add(childList);
                                parent = childList;
                                lastList = parent;
                            }
                            else
                            {
                                parent = lastList;
                                lastList = parent.AddChildList(text);
                            }
                        }
                        else if (tabs < lastTabLevel)
                        {
                            parent = parent.Parent;
                            if (parent == null)
                                parent = m_List;
                            lastList = parent.AddChildList(text);
                        }
                        else
                        {
                            lastList = parent.AddChildList(text);
                        }
                    }
                    lastTabLevel = tabs;
                }
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private int CountTabsForSegment(ICSharpCode.TextEditor.Document.LineSegment segment)
        {
            int numberOfTabs = 0;
            for (int j = 0; j < segment.Words.Count; j++)
            {
                if (segment.Words[j].Type == ICSharpCode.TextEditor.Document.TextWordType.Tab)
                {
                    numberOfTabs++;
                }
                else
                {
                    break;
                }
            }
            return numberOfTabs;
        }

    }
}
